<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="${table.tableNameCamel}Mapper">
    <select id="selectByPrimaryKey" resultType="${table.tableNameCamel}PO" parameterType="map">
        select <include refid="base_column_list"/>
        from ${table.tableNameSnake} where <#list table.primaryKey as pk>${table.fields?filter(f -> f.nameCamel == pk)[0].nameSnake} = #{${pk}}<#if pk_has_next> and </#if></#list>
    </select>

    <select id="selectByPage" resultType="${table.tableNameCamel}PO">
        select <include refid="base_column_list"/>
        from ${table.tableNameSnake} where <include refid="where_condition"/>
    </select>

    <select id="selectResultHandler" resultType="${table.tableNameCamel}PO">
        select <include refid="base_column_list"/>
        from ${table.tableNameSnake} where <include refid="where_condition"/>
    </select>

    <delete id="deleteByPrimaryKey" parameterType="map">
        delete from ${table.tableNameSnake} where <#list table.primaryKey as pk>${table.fields?filter(f -> f.nameCamel == pk)[0].nameSnake} = #{${pk}}<#if pk_has_next> and </#if></#list>
    </delete>

    <update id="updateByPrimaryKey" parameterType="${table.tableNameCamel}PO">
        update ${table.tableNameSnake} set
        <#list table.fields as field>
        <#if !(table.primaryKey?seq_contains(field.nameCamel))>${field.nameSnake}=#{${field.nameCamel}}<#if field_has_next>, </#if></#if>
        </#list>
        where <#list table.primaryKey as pk>${table.fields?filter(f -> f.nameCamel == pk)[0].nameSnake} = #{${pk}}<#if pk_has_next> and </#if></#list>
    </update>

    <update id="updateByPrimaryKeySelective" parameterType="${table.tableNameCamel}PO">
        update ${table.tableNameSnake}
        <trim prefix="set" suffixOverrides=",">
            <#list table.fields as field>
            <#if !(table.primaryKey?seq_contains(field.nameCamel))>
            <if test="${field.nameCamel} != null<#if field.type=="String"> and ${field.nameCamel} != ''</#if>">${field.nameSnake}=#{${field.nameCamel}},</if>
            </#if>
            </#list>
        </trim>
        where <#list table.primaryKey as pk>${table.fields?filter(f -> f.nameCamel == pk)[0].nameSnake} = #{${pk}}<#if pk_has_next> and </#if></#list>
    </update>

    <insert id="insert" parameterType="${table.tableNameCamel}PO">
        insert into ${table.tableNameSnake}(
            <#list table.fields as field>${field.nameSnake}<#if field_has_next>, </#if></#list>
        ) values (
            <#list table.fields as field>#{${field.nameCamel}}<#if field_has_next>, </#if></#list>
        )
    </insert>

    <insert id="insertSelective" parameterType="${table.tableNameCamel}PO">
        insert into ${table.tableNameSnake}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
            <if test="${field.nameCamel} != null<#if field.type=="String"> and ${field.nameCamel} != ''</#if>">${field.nameSnake},</if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list table.fields as field>
            <if test="${field.nameCamel} != null<#if field.type=="String"> and ${field.nameCamel} != ''</#if>">#{${field.nameCamel}},</if>
            </#list>
        </trim>
    </insert>

    <sql id="base_column_list">
        <#list table.fields as field>${field.nameSnake}<#if field_has_next>,</#if></#list>
    </sql>

    <sql id="where_condition">
        <trim prefix="where" prefixOverrides="and">
            <#list table.fields as field>
            <if test="${field.nameCamel} != null<#if field.type=="String"> and ${field.nameCamel} != ''</#if>"> and ${field.nameSnake}=#{${field.nameCamel}} </if>
            </#list>
        </trim>
    </sql>
</mapper>

<#--
示例数据结构：
{
  "table": {
    "tableNameCamel": "AcmCddt",
    "tableNameSnake": "t_acm_cddt",
    "tableCommentCn": "存款收支明细表",
    "tableCommentEn": "Deposit and Withdrawal Details Table",
    "fields": [
      {"nameCamel": "jrnNo", "nameSnake": "jrn_no", "type": "String", "commentCn": "流水号", "commentEn": "Journal number"},
      {"nameCamel": "sysDt", "nameSnake": "sys_dt", "type": "String", "commentCn": "系统日期", "commentEn": "System date"}
    ],
    "primaryKey": ["jrnNo", "jrnSeq", "acDt"],
    "indexes": [
      {"name": "NI1_T_ACM_CDDT", "fields": ["sepCd", "acDt"]},
      {"name": "NI2_T_ACM_CDDT", "fields": ["sysDt", "sepCd"]}
    ]
  }
}
--> 